package com.lensyn.generator.handler;

import org.mybatis.generator.internal.util.JavaBeansUtil;

import java.util.Properties;

/**
 * @author xiaob@lensysn.com
 * @date 2016/12/20
 */
public class DefaultHandlerAdapter implements DtoNameHandler, ControllerNameHandler, ServiceNameHandler,EntityMetaHandler {

    String basePackage;

    Properties moduleMapping = new Properties();

    public Properties getModuleMapping() {
        return moduleMapping;
    }

    public void setModuleMapping(Properties moduleMapping) {

        if (moduleMapping != null) {
            this.moduleMapping.putAll(moduleMapping);
        }
    }

    public void setPackageName(String packageName) {
        basePackage = packageName;
    }

    ;

    public String getPackageName() {
        return this.basePackage;
    }

    ;

    public String basePackageName() {
        return getPackageName();
    }

    public String moduleName(String tableName) {
        String[] strArr = tableName.toLowerCase().split("_");
        String module = "";
        if (strArr.length > 1) {
            module = moduleMapping.getProperty(strArr[0], strArr[0]);
        }
        return module;
    }

    public String dtoName(String tableName) {
        StringBuffer sb = new StringBuffer();

        String packageName = basePackageName();
        if (packageName != null && !packageName.isEmpty()) {
            sb.append(packageName);
            sb.append(".");
        }

        sb.append("domain");
        sb.append(".");
        sb.append(JavaBeansUtil.getCamelCaseString(tableName, true));
        sb.append("Dto");

        return sb.toString();
    }

    public String controllerName(String tableName) {
        StringBuffer sb = new StringBuffer();
        String packageName = basePackageName();
        if (packageName != null && !packageName.isEmpty()) {
            sb.append(packageName);
            sb.append(".");
        }
        sb.append("controller");
        sb.append(".");

        String module = moduleName(tableName);
        if (module != null && !module.isEmpty()) {
            sb.append(module);
            sb.append(".");
        }

        sb.append(JavaBeansUtil.getCamelCaseString(tableName, true));
        sb.append("Controller");
        return sb.toString();
    }

    public String serviceName(String tableName) {
        StringBuffer sb = new StringBuffer();
        String packageName = basePackageName();
        if (packageName != null && !packageName.isEmpty()) {
            sb.append(packageName);
            sb.append(".");
        }
        sb.append("service");
        sb.append(".");
        String module = moduleName(tableName);
        if (module != null && !module.isEmpty()) {
            sb.append(module);
            sb.append(".");
        }

        sb.append(JavaBeansUtil.getCamelCaseString(tableName, true));
        sb.append("Service");
        return sb.toString();
    }

    public String serviceImlName(String tableName) {
        StringBuffer sb = new StringBuffer();
        String packageName = basePackageName();
        if (packageName != null && !packageName.isEmpty()) {
            sb.append(packageName);
            sb.append(".");
        }
        sb.append("service");
        sb.append(".");

        String module = moduleName(tableName);
        if (module != null && !module.isEmpty()) {
            sb.append(module);
            sb.append(".");
        }

        sb.append("impl");
        sb.append(".");

        sb.append(JavaBeansUtil.getCamelCaseString(tableName, true));
        sb.append("ServiceImpl");

        return sb.toString();
    }

    public String entityMetaName(String tableName) {
        StringBuffer sb = new StringBuffer();

        String packageName = basePackageName();
        if (packageName != null && !packageName.isEmpty()) {
            sb.append(packageName);
            sb.append(".");
        }

        sb.append("entity");
        sb.append(".");
        sb.append("meta");
        sb.append(".");
        sb.append(JavaBeansUtil.getCamelCaseString(tableName, true));
        sb.append("Meta");

        return sb.toString();
    }
}
